﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class User_Edit : BasePage
    {
        protected Guid userId
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }
        protected Guid depId
        {
            get { return this.GetUrlParam("depId") == null ? Guid.Empty : new Guid(this.GetUrlParam("depId").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (userId != Guid.Empty)
                    BindData();
                if (depId != Guid.Empty)
                    hyplBack.NavigateUrl = string.Format("{0}User/Organization.aspx?id={1}", AppPath, depId);
            }
        }

        private void BindData()
        {
            liTitle.Text = "修改";
            using (UsersBLL userB = new UsersBLL())
            {
                Users user = userB.GetEntity(t => t.Id == userId);
                tbCode.Text = user.Code.Trim();
                tbICCard.Text = user.ICCard.Trim();
                tbName.Text = user.Name.Trim();
                tbIdNumber.Text = user.IdNumber.Trim();
                tbHomeTown.Text = user.HomeTown.Trim();
                tbArchives.Text = user.Archives.Trim();
                this.rbGender.SelectedIndex = (int)user.Gender;
                tbBirthday.Text = user.Birthday == null ? "" : user.Birthday.Value.ToString("yyyy-MM-dd");
                tbTel.Text = user.Tel.Trim();
                tbMobile.Text = user.Mobile.Trim();
                tbEmail.Text = user.Email.Trim();
                if (!string.IsNullOrEmpty(user.Photo))
                {
                    Image1.Visible = true;
                    Image1.ImageUrl = "../UserPhoto/" + user.Photo;
                }
                tbAddress.Text = user.Address.Trim();
            }
        }

        protected void btnSave_OnClick(object sender, ImageClickEventArgs e)
        {
            bool result = false;
            using (UsersBLL uBLL = new UsersBLL())
            {
                if (uBLL.GetList(t => t.Id != userId && t.Code == tbCode.Text.Trim()).Count > 0)
                {
                    lbResult.Text = "工号或ID卡号已被使用，请重新输入";
                    return;
                }
                if (!string.IsNullOrEmpty(tbICCard.Text.Trim()) && uBLL.GetList(t => t.Id != userId && t.ICCard == tbICCard.Text.Trim()).Count > 0)
                {
                    lbResult.Text = "ID卡号已被使用，请重新输入";
                    return;
                }
                Users _user = new Users();
                if (userId != Guid.Empty)
                {
                    _user = uBLL.GetEntity(t => t.Id == userId);
                }
                else
                {
                    if (string.IsNullOrEmpty(tbPassword.Text.Trim()))
                    {
                        lbResult.Text = "请输入密码";
                        return;
                    }
                    _user.Id = Guid.NewGuid();
                }
                if (!string.IsNullOrEmpty(tbPassword.Text.Trim()))
                    _user.Password = MD5Helper.EncryptString(tbPassword.Text.Trim());
                _user.Code = tbCode.Text.Trim();
                _user.ICCard = tbICCard.Text.Trim();
                _user.Name = tbName.Text.Trim();
                _user.IdNumber = tbIdNumber.Text.Trim();
                _user.HomeTown = tbHomeTown.Text.Trim();
                _user.Archives = tbArchives.Text.Trim();
                _user.Gender = int.Parse(this.rbGender.SelectedValue);
                if (!string.IsNullOrEmpty(this.tbBirthday.Text.Trim()))
                    _user.Birthday = DateTime.Parse(this.tbBirthday.Text.Trim());
                _user.Tel = tbTel.Text.Trim();
                _user.Mobile = tbMobile.Text.Trim();
                _user.Email = tbEmail.Text.Trim();
                _user.Address = tbAddress.Text.Trim();
                if (PhotoFileUpload.HasFile)
                {
                    string SaveUrl = string.Format("{0}/UploadFiles/UserPhoto/", Server.MapPath(AppPath));
                    if (!string.IsNullOrEmpty(_user.Photo))
                        Utility.DeleteFile(SaveUrl + _user.Photo);
                    _user.Photo = Utility.MakeMiniature(PhotoFileUpload.PostedFile, SaveUrl, 160, 200, "W");
                    Image1.Visible = true;
                    Image1.ImageUrl = "../UploadFiles/UserPhoto/" + _user.Photo;
                }
                if (depId == null)
                {
                    result = uBLL.SaveEntity(_user, null) == 1 ? true : false;
                    return;
                }
                else
                {
                    if (userId != Guid.Empty)
                    {
                        result = uBLL.Update(_user);
                    }
                    else
                    {
                        using (UserInDepartmentsBLL udBLL = new UserInDepartmentsBLL())
                        {
                            UserInDepartments entity = new UserInDepartments();
                            entity.DepartmentId = depId;
                            entity.IsManager = 0;
                            entity.Users = _user;
                            result = udBLL.Insert(entity);
                        }
                    }
                }
            }
            if (result)
            {
                btnSave.Visible = false;
                lbResult.Text = "保存成功";
            }
            else
                lbResult.Text = "保存失败";
        }
    }
}